/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class ApplicationTask.
//
// ApplicationTask is a abstract unit used to communicate between application layer
// and network layer.
//

#include "application_task.h"
#include "logging.h"
#include "ring_queue_single_model_bound.h"
#include "ring_queue_single_model_boundless.h"
#include "ring_queue_multiple_model_bound.h"
#include "ring_queue_multiple_model_boundless.h"

namespace amz {

ApplicationTask::ApplicationTask(
       const ring_queue_define::RingQueueConfigure &configure) :
        message_queue_send_(NULL), message_queue_receive_(NULL) {
  this->InitializeRingQueue(configure);
}

ApplicationTask::~ApplicationTask() {
  this->FinalizeRingQueue();
}

int ApplicationTask::SendMessage(const char *message, int size) {
  return this->message_queue_send_->Add(message, size);
}

int ApplicationTask::ReceiveMessage(char *message, int max_size) {
  return this->message_queue_receive_->Remove(message, size);
}

void ApplicationTask::InitializeRingQueue(
       const ring_queue_define::RingQueueConfigure &configure) {
  // Allocate heap memory to send/receive ring queue.
  switch(configure.type_)
    case RingQueueType_SingleBound: {
      this->message_queue_send_= new RingQueueSingleModelBound(
             configure.ring_size_, configure.blocking_);
      this->message_queue_receive_= new RingQueueSingleModelBound(
             configure.ring_size_, configure.blocking_);
      break;
    case RingQueueType_SingleBoundless:
      this->message_queue_send_= new RingQueueSingleModelBoundless(
             configure.ring_size_, configure.dynamic_queue_size_,
             configure.blocking_);
      this->message_queue_receive_= new RingQueueSingleModelBoundless(
             configure.ring_size_, configure.dynamic_queue_size_,
             configure.blocking_); 
      break;
    case RingQueueType_MultipleBound:
      this->message_queue_send_ = new RingQueueMultipleModelBound(
             configure.number_of_producers_, configure.ring_size_, 
             configure.blocking_);
      this->message_queue_receive_ = new RingQueueMultipleModelBound(
             configure.number_of_producers_, configure.ring_size_, 
             configure.blocking_);
      break;
    case RingQueueType_MultipleBoundless:
      this->message_queue_send_ = new RingQueueMultipleModelBoundless(
             configure.number_of_producers_, configure.ring_size_,
             configure.dynamic_queue_size_, configure.blocking_);
      this->message_queue_receive_ = new RingQueueMultipleModelBoundless(
             configure.number_of_producers_, configure.ring_size_,
             configure.dynamic_queue_size_, configure.blocking_);
      break;
    default: {
      LOG(ERROR) << "Error ring queue type.";
    }
  }
}

void ApplicationTask::FinalizeRingQueue() {
  if(this->message_queue_send_ != NULL) {
    delete this->message_queue_send_;
    this->message_queue_send_ = NULL;
  }
  if(this->message_queue_receive_ != NULL) {
    this->message_queue_receive_ = NULL;
  }
}

}  // namespace amz

#endif  // __APPLICATION__TASK__H

